package com.whsxt.buss.controller;


import java.io.File;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.buss.data.GoodsData;
import com.whsxt.buss.domain.BusGoods;
import com.whsxt.buss.domain.BusProvider;
import com.whsxt.buss.service.IBusGoodsService;
import com.whsxt.buss.service.IBusProviderService;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;
import com.whsxt.common.util.FileUpdateUtil;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-27
 */
@RestController
@RequestMapping("/buss/goods")
public class BusGoodsController {
	
	@Autowired
	private IBusGoodsService iBusGoodsService;
	
	@Autowired
	private IBusProviderService iBusProviderService;
	/**
	 * 	获取商品列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(GoodsData data) {
		IPage<BusGoods> page = new Page<BusGoods>(data.getPage(), data.getLimit());
		QueryWrapper<BusGoods> queryWrapper = new QueryWrapper<BusGoods>();
		queryWrapper.eq(data.getProviderid() != null &&data.getProviderid() != ' ' , "providerid", data.getProviderid());
		queryWrapper.like(StrUtil.isNotBlank(data.getGoodsname()), "goodsname", data.getGoodsname());
		queryWrapper.like(StrUtil.isNotBlank(data.getProductcode()), "productcode", data.getProductcode());
		queryWrapper.like(StrUtil.isNotBlank(data.getPromitcode()), "promitcode", data.getPromitcode());
		queryWrapper.like(StrUtil.isNotBlank(data.getDescription()), "description", data.getDescription());
		queryWrapper.like(StrUtil.isNotBlank(data.getSize()), "size", data.getSize());
		iBusGoodsService.page(page, queryWrapper);
		List<BusGoods> records = page.getRecords();
		for (BusGoods busGoods : records) {
			Integer providerid = busGoods.getProviderid();
			BusProvider byId = iBusProviderService.getById(providerid);
			busGoods.setProvidername(byId.getProvidername());
		}
		PageInfo pageInfo = new PageInfo(page.getTotal(), records);
		return new Result(pageInfo);
	}
	/**
	 * 	获取所有的供应商
	 * @return
	 */
	@RequestMapping("/getProvider")
	public Object getProvider() {
		List<BusProvider> list = iBusProviderService.list();
		return new Result(list);
	}
	
	/**
	 * 	获取所有的商品
	 * @return
	 */
	@RequestMapping("/getGoods")
	public Object getGoods(Integer providerid) {
		QueryWrapper<BusGoods> queryWrapper = new QueryWrapper<BusGoods>();
		queryWrapper.eq(providerid!=null && providerid!= ' ',"providerid", providerid);
		List<BusGoods> list = iBusGoodsService.list(queryWrapper );
		return new Result(list);
	}
	
	/**
	 * 	新增商品
	 * @param goods
	 * @return
	 */
	@RequestMapping("/addBusGoods")
	public Object addBusGoods(BusGoods goods) {
		String goodsimg = goods.getGoodsimg();
		if(goodsimg!=null && goodsimg.endsWith("_temp")) {
			File file = new File(FileUpdateUtil.diskPath,goodsimg);
			goodsimg = goodsimg.replace("_temp", "");
			//将带有_temp后缀的文件替换成没有_temp
			file.renameTo(new File(FileUpdateUtil.diskPath, goodsimg));
		}
		goods.setGoodsimg(goodsimg);
		
		try {
			iBusGoodsService.save(goods);
			return new Result(CodeMsg.GOODS_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.GOODS_ADD_ERROR);
		}
	}
	/**
	 * 	 更新商品数据
	 * @param goods
	 * @return
	 */
	@RequestMapping("/updateBusGoods")
	public Object updateBusGoods(BusGoods goods) {
		String goodsimg = goods.getGoodsimg();
		if(!(goodsimg!=null && goodsimg.equals(ConstantUtil.DEFAULT_IMG))) {
			File file = new File(FileUpdateUtil.diskPath,goodsimg);
			goodsimg = goodsimg.replace("_temp", "");
			//将带有_temp后缀的文件替换成没有_temp
			file.renameTo(new File(FileUpdateUtil.diskPath, goodsimg));
			goods.setGoodsimg(goodsimg);
			//获取之前图片的路径
			String goodsimg2 = this.iBusGoodsService.getById(goods.getId()).getGoodsimg();
			//判断之前的图片路径是不是默认的图片路径 如果不是就删除之前的图片  如果是默认图片就不删除
			FileUpdateUtil.deleteFileByimgpath(goodsimg2);
		}
		try {
			iBusGoodsService.updateById(goods);
			return new Result(CodeMsg.GOODS_UPDATE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.GOODS_UPDATE_ERROE);
		}
	}
	/**
	 *   删除商品  如果商品不是默认图片 就删除图片  是默认图片就不删除
	 * @param id
	 * @param goodsimg
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id,String goodsimg) {
		try {
			FileUpdateUtil.deleteFileByimgpath(goodsimg);
			iBusGoodsService.removeById(id);
			return new Result(CodeMsg.GOODS_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.GOODS_DELETE_ERROE);
		}
	}
}

